/*
 * Copyright (c) 2017. 深圳掌控网络有限公司. All rights reserved.
 */

package com.yiqi.lottery.feature.mine.presenter;

import android.text.TextUtils;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.ValidatorUtil;
import com.yiqi.lottery.entity.bo.RegisterBo;
import com.yiqi.lottery.feature.mine.contract.BindContract;
import com.yiqi.lottery.feature.mine.contract.MineContract;
import com.yiqi.lottery.feature.mine.dependencies.bind.DaggerBindPresenterComponent;
import com.yiqi.lottery.feature.mine.dependencies.mine.DaggerMinePresenterComponent;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import javax.inject.Inject;

/**
 *      LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class BindPresenter implements BindContract.Presenter {

    private BindContract.View mView;

    @Inject

    BindContract.Model mModel;


    public BindPresenter(BindContract.View mView) {
        this.mView = mView;
        DaggerBindPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void doGetOldPhoneVerify(String phone, String code) {
        if (!ValidatorUtil.isMobile(phone)) {
            T.showShort("手机号格式有误!");
            return;
        }
        if (TextUtils.isEmpty(code) && code.length() != 6) {
            T.showShort("验证码格式有误!");
            return;
        }
        RegisterBo registerBo = new RegisterBo();
        registerBo.setMobile(phone);
        registerBo.setCode(code);
        mModel.getOldPhoneVerify(registerBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showOldPhoneVerifySuccess(s);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showOldPhoneVerifyError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void doUserBindSendCode(String mobile) {
        if (!ValidatorUtil.isMobile(mobile)) {
            T.showShort("手机号格式有误!");
            return;
        }
        mModel.userBindSendCode(mobile).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showSendCodeSuccess(s);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showSendCodeError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void doGetNewPhoneVerify(String phone, String code) {
        if (!ValidatorUtil.isMobile(phone)) {
            T.showShort("手机号格式有误!");
            return;
        }
        if (TextUtils.isEmpty(code) && code.length() != 6) {
            T.showShort("验证码格式有误!");
            return;
        }

        RegisterBo registerBo = new RegisterBo();
        registerBo.setMobile(phone);
        registerBo.setCode(code);
        mModel.getNewPhoneVerify(registerBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showNewPhoneVerifySuccess(s);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showNewPhoneVerifyError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
